package com.cjj2016.cjj.wandoujiaanimationdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cjj2016.cjj.wandoujiaanimationdemo.R;

/**
 * Created by CJJ on 2017/1/13.
 */

public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;

    private Object[][] mArrays;

    private MyOnItemClickListener myOnItemClickListener;

    public MyRecyclerAdapter(Context context,Object[][] arrays) {
        mContext=context;
        mArrays = arrays;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.activity_list_item,null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder itemVH = (MyViewHolder) holder;
        Object[] contentArray = mArrays[position];
        itemVH.imageViewIcon.setImageResource((int) contentArray[0]);
        itemVH.textviewAppname.setText((String)contentArray[1]);

        if(myOnItemClickListener !=null){
            itemVH.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myOnItemClickListener.onItemClick(v,position);
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return mArrays.length;
    }

    public void setMyOnItemClickListener(MyOnItemClickListener listener){
        myOnItemClickListener = listener;
    }



    public interface MyOnItemClickListener{
        void onItemClick(View view,int position);
    }



    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView imageViewIcon;
        TextView textviewAppname;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageViewIcon = (ImageView) itemView.findViewById(R.id.imageview_icon);
            textviewAppname= (TextView) itemView.findViewById(R.id.textview_appname);
        }
    }





}
